package com.dx.mes.service.special.service;

import java.sql.Timestamp;
import java.text.Format;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import com.dx.mes.common.bean.erp.EWOWHIN;
import org.apache.commons.lang3.StringUtils;
import org.hibernate.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import com.dx.common.table.ppt.Bis_data;
import com.dx.common.table.ppt.Ret_box;
import com.dx.common.table.ppt.Ret_prd_abn;
import com.dx.common.table.ppt.Ret_prd_abnId;
import com.dx.common.table.ppt.Ret_prd_info;
import com.dx.common.table.ppt.Ret_wo;
import com.dx.mes.common.bean.erp.EWOSCRP;
import com.dx.mes.common.core.rabbitmq.BaseConnFactory;
import com.dx.mes.common.dao.IBisDataDao;
import com.dx.mes.common.dao.IRetBoxDao;
import com.dx.mes.common.dao.IRetPrdAbnDao;
import com.dx.mes.common.dao.IRetPrdInfoDao;
import com.dx.mes.common.dao.IRetWODao;
import com.dx.mes.common.table.rtnBean.NotFoundRtnBeen;
import com.dx.mes.common.table.rtnBean.SimpleRtnBeen;
import com.dx.mes.common.transcoder.util.AppContext;
import com.dx.mes.common.transcoder.util.GUIDGenerator;
import com.dx.mes.common.transcoder.util.JacksonUtil;
import com.dx.mes.common.transcoder.util.LogUtils;
import com.dx.mes.common.transcoder.util.Util;
import com.dx.mes.facade.special.service.IFspprdscrapService;
import com.dx.mes.facade.special.tx.Fspprdscrap.FspprdscrapI;
import com.dx.mes.facade.special.tx.Fspprdscrap.FspprdscrapIA;
import com.dx.mes.facade.special.tx.Fspprdscrap.FspprdscrapO;
import sun.util.resources.cldr.bn.CalendarData_bn_IN;

import static com.dx.mes.common.bean.def.GenericDef.*;

@Service("fspprdscrapService")
@SuppressWarnings({"unchecked"})
public class FspprdscrapService implements IFspprdscrapService{
	private LogUtils logUtils;
	private static String TX_NAME = T_FSPPRDSCRP;

	@Autowired
	private IRetBoxDao retBoxDao;
	@Autowired
	private IRetPrdInfoDao retPrdInfoDao;
	@Autowired
	private IRetWODao retWoDao;
	@Autowired
	private IRetPrdAbnDao retPrdAbnDao;
	@Autowired
	private IBisDataDao bisDataDao;
	@Resource
	private BaseConnFactory baseConnFactory;

	@Override
	public String subMainProc(String evt_no, String strInTrx) {
		AppContext.clear();
		AppContext.setCurrServiceName(FspprdscrapService.class.getSimpleName());
		AppContext.setCurrEventNumber(evt_no);
		logUtils = new LogUtils(FspprdscrapService.class);
		logUtils.info("[InTrx:" + strInTrx + "]");

		Session session = retWoDao.getCrSession();
		FspprdscrapO outTrx = new FspprdscrapO();
		String strOutTrx = null;

		outTrx.setTrx_id(TX_NAME);
		outTrx.setType_id(TRX_OUT);
		outTrx.setRtn_code(String.valueOf(RETURN_CODE_SET_ERROR));

		//输入对象
		try {
			FspprdscrapI inTrx = new FspprdscrapI();
			//将json字符串转换成对象
			inTrx = JacksonUtil.fromJson(strInTrx, FspprdscrapI.class);
			char action_flg = inTrx.getAction_flg().charAt(0);

			switch (action_flg) {
				case 'S':
					scrapByPrdFnc(inTrx, outTrx);
					break;
				case 'B':
					scrapByCstFnc(inTrx, outTrx);
					break;
				case 'C':
					cancelFnc(inTrx, outTrx);
					break;
				default:
					errActionFnc(outTrx);
			}
			if (RETURN_CODE_OK.equals(outTrx.getRtn_code())) {
				session.flush();
			} else {
				TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
			}
		}catch (Exception ex) {
			outTrx.setRtn_code(RETURN_CODE_UNKNOWN);
			outTrx.setRtn_mesg(ex.toString());
			TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
		} finally {
			//将一个 Object 或者List对象转化为JSOObject或者JSONArray
			strOutTrx = JacksonUtil.toJSONStr(outTrx);
			logUtils.info("[OutTrx:" + strOutTrx + "]");
		}
		return strOutTrx;
	}

	private void scrapByCstFnc(FspprdscrapI inTrx, FspprdscrapO outTrx) {
		// TODO Auto-generated method stub
		String evtUsr = inTrx.getEvt_usr();
		Timestamp evt_timestamp = Util.getCurrentTimestamp();
		String evt_seq_id = GUIDGenerator.javaGUID();
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		List<Ret_prd_info> ret_prd_infos = new ArrayList<Ret_prd_info>();

		String box_id = inTrx.getBox_id();
		if(StringUtils.isEmpty(box_id)){
			outTrx.setRtn_code(E_FSPPRDSCRP_OTHER_ERR + "");
			outTrx.setRtn_mesg("箱号["+box_id+"]为空，请确认!");
			return;
		}
		Ret_box ret_box = retBoxDao.get(box_id);
		if(ret_box == null){
			outTrx.setRtn_code(E_RET_BOX + E_READ_NOT_FOUND + "");
			outTrx.setRtn_mesg("箱号["+box_id+"]不存在，请确认!");
			return;
		}
		ret_prd_infos = retPrdInfoDao.getShtListInfo(box_id);
		if(ret_prd_infos == null){
			outTrx.setRtn_code(E_FSPPRDSCRP_OTHER_ERR + "");
			outTrx.setRtn_mesg("箱子["+box_id+"]中没有玻璃，请确认!");
			return;
		}
		for(Ret_prd_info ret_prd_info : ret_prd_infos){
			if( !PROC_WAIT.equals(ret_prd_info.getPrd_stat().trim()) ){
				outTrx.setRtn_code(E_FSPPRDSCRP_OTHER_ERR + "");
				outTrx.setRtn_mesg("整卡匣报废，玻璃状态必须是[WAIT]，"
						+ "而玻璃/面板["+ret_prd_info.getPrd_seq_id()+"]状态是["+ret_prd_info.getPrd_stat()+"],请确认!");
				return;
			}

			/**
			 * set prd_seq_id information
			 */
			setPrdScrapInfo(evt_seq_id, inTrx, ret_prd_info, evt_timestamp);
		}
		/**
		 * set box empty
		 */
		retBoxDao.setBoxEmpt(ret_box);
		//add wo && so scrap qty
		rtnBeen = updateWoSoScrapQty("B",ret_prd_infos.get(0).getWo_id_fk(), ret_prd_infos.get(0).getSo_id_fk(),
				ret_prd_infos.size(), evt_timestamp, inTrx.getEvt_usr());
		if (rtnBeen.getlRc() != _NORMAL) {
			outTrx.setRtn_code(rtnBeen.getlRc()+"");
			outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
			return;
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_CODE_OK);
	}


	public void scrapByPrdFnc(FspprdscrapI inTrx ,FspprdscrapO outTrx){
		Timestamp evt_timestamp = Util.getCurrentTimestamp();
		String evt_seq_id = GUIDGenerator.javaGUID();
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen(_NORMAL);
		String  mesRsvNo = "S"+ GUIDGenerator.javaGUID().substring(0,11);
		List<Ret_prd_info> prdInfoList = new ArrayList<>();
		for(int i=0;i<inTrx.getIary().size();i++){
			FspprdscrapIA iary = inTrx.getIary().get(i);
			String prdSeqId = iary.getPrd_seq_id();


			if(StringUtils.isEmpty(prdSeqId)){
				outTrx.setRtn_code(E_FSPPRDSCRP_OTHER_ERR + "");
				outTrx.setRtn_mesg("玻璃/面板["+prdSeqId+"]为空，请确认!");
				return;
			}

			Ret_prd_info ret_prd_info = retPrdInfoDao.get(prdSeqId);
			if(ret_prd_info == null){
				outTrx.setRtn_code(E_RET_PRD_INFO + E_READ_NOT_FOUND + "");
				outTrx.setRtn_mesg("玻璃/面板["+prdSeqId+"]不存在，请确认!");
				return;
			}
			if( !PROC_WAIT.equals(ret_prd_info.getPrd_stat().trim()) ){
				outTrx.setRtn_code(E_FSPPRDSCRP_OTHER_ERR + "");
				outTrx.setRtn_mesg("单片玻璃报废，玻璃状态必须是[" + PROC_WAIT + "]，"
						+ "而玻璃/面板["+prdSeqId+"]状态是["+ret_prd_info.getPrd_stat()+"],请确认!");
				return;
			}
			ret_prd_info.setPrd_stat(PROC_SCRP);
			ret_prd_info.setBnk_flg(BANK_IN);
			ret_prd_info.setRsv_no(mesRsvNo);
			ret_prd_info.setDest_shop(inTrx.getDest_shop());

			ret_prd_info.setRsn_cate(inTrx.getRsn_cate());
			ret_prd_info.setRsn_code(inTrx.getRsn_code());
			ret_prd_info.setRsn_dept(inTrx.getRsn_dept());
			ret_prd_info.setSht_ope_msg(inTrx.getSht_ope_msg());

			ret_prd_info.setEvt_cate(EVT_PRD_SCRP);
			ret_prd_info.setEvt_usr(inTrx.getEvt_usr());
			ret_prd_info.setEvt_timestamp(evt_timestamp);
			prdInfoList.add(ret_prd_info);
			//更新屏体新和写入ret_prd_adb 以及更新工单中报废屏体的数量操作，在SAP完成后完成
			/*setPrdScrapInfo(evt_seq_id, inTrx, ret_prd_info, evt_timestamp);
			rtnBeen = updateWoSoScrapQty("B",ret_prd_info.getWo_id_fk(), ret_prd_info.getSo_id_fk(), 1, evt_timestamp, inTrx.getEvt_usr());
			if (rtnBeen.getlRc() != _NORMAL) {
				outTrx.setRtn_code(rtnBeen.getlRc()+"");
				outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
				return;
			}*/
		}
		retPrdInfoDao.saveOrUpdateAll(prdInfoList);
		//信息传给SAP
		if(inTrx.getIary().size()>0){
			EWOSCRP ewoscrp = new EWOSCRP();
			Ret_prd_info ret_prd_info = retPrdInfoDao.get(inTrx.getIary().get(0).getPrd_seq_id());
			List<EWOSCRP> ewoscrpList = new ArrayList<EWOSCRP>();
			if(ret_prd_info!=null){
				ewoscrp.setAUFNR(ret_prd_info.getWo_id_fk());
				//根据报废站点应该对应相应的工序
				String hql = "FROM Bis_data where data_cate ='DWPR' and ext_2='" + ret_prd_info.getMdl_id_fk() + "' and data_ext ='" + ret_prd_info.getCr_ope_id_fk() + "'";
				List<Bis_data> bis_dataList = bisDataDao.find(hql);
				if(bis_dataList!=null && bis_dataList.size()>0){
					ewoscrp.setVORNR(bis_dataList.get(0).getExt_5());
				}
				ewoscrp.setXMNGA(String.valueOf(inTrx.getIary().size()));
				ewoscrp.setZBFSB(ret_prd_info.getCr_ope_id_fk());
				Format dateFormat = new SimpleDateFormat("yyyyMMdd");
				String date  = dateFormat.format(new Date());
				Format dateFormat1 = new SimpleDateFormat("HHmmss");
				String time  = dateFormat1.format(new Date());
				ewoscrp.setZBFSJ(time);
				ewoscrp.setZBFRQ(date);
				ewoscrp.setZBFYY(inTrx.getRsn_code());
				ewoscrp.setZBFRY(inTrx.getEvt_usr());
				ewoscrp.setZBFBM(inTrx.getRsn_dept());
				ewoscrp.setZBFZT("SCRP");
				ewoscrpList.add(ewoscrp);
				/*try {
					baseConnFactory.addSAPREQProc("EWOSCRP", JacksonUtil.toJSONStr(ewoscrpList));
				}catch (Exception e){
					e.printStackTrace();
				}*/
			}
			//再发一个报废入库动作
			List<EWOWHIN> ewowhinList = new ArrayList<EWOWHIN>();
			EWOWHIN ewowhin = new EWOWHIN();
			Ret_wo ret_wo = retWoDao.get(ret_prd_info.getWo_id_fk());
			ewowhin.setAUFNR(ret_wo.getWo_id());
			ewowhin.setAUART(ret_wo.getWo_cate());
			ewowhin.setWEMPF(mesRsvNo);
			ewowhin.setMATNR(inTrx.getMtrl_prod_id());
			ewowhin.setMENGE(inTrx.getIary().size());
			ewowhin.setMEINS("PCS");
			ewowhin.setCHARG("S"+ret_wo.getWo_id());//没有批次号，再传一次工单
			ewowhin.setZBWART("Z05");
			ewowhin.setBWART("Z14");
			ewowhin.setWERKS(ret_wo.getFab_id_fk());//工厂
			String hqlA ="FROM Bis_data where data_cate ='DEST' and data_ext ='" + ret_prd_info.getDest_shop() + "'";
			List<Bis_data> destList = bisDataDao.find(hqlA);
			if(destList!=null && destList.size()>0){
				ewowhin.setLGORT(destList.get(0).getExt_2());//接受仓位
			}
			ewowhin.setUMLGO(ret_prd_info.getDest_shop());//发出库存地址
			Format dateFormat = new SimpleDateFormat("yyyyMMdd");
			String date  = dateFormat.format(new Date());
			ewowhin.setHSDAT(date);
			ewowhin.setBUDAT(date);
			ewowhinList.add(ewowhin);
			/*try {
				baseConnFactory.addSAPREQProc("EWOWHIN", JacksonUtil.toJSONStr(ewowhinList));
			}catch (Exception e){
				e.printStackTrace();
			}*/
		}
		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_CODE_OK);
	}

	/**
	 * update wo && so scrap qty
	 * @param wo_id
	 * @param so_id
	 * @param scrap_qty
	 * @param evt_timestamp
	 * @param evt_user
	 * @return
	 */
	private SimpleRtnBeen updateWoSoScrapQty(String flag,String wo_id, String so_id, Integer scrap_qty, Timestamp evt_timestamp, String evt_user){
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen();
		rtnBeen.setlRc(_NORMAL);
		// update wo scrap count
		Ret_wo ret_wo = retWoDao.get(wo_id);
		if (null == ret_wo) {
			rtnBeen.setMsgBuf(new NotFoundRtnBeen(wo_id, Ret_wo.class).toString());
			rtnBeen.setlRc(E_RET_WO + E_READ_NOT_FOUND);
			return rtnBeen;
		}
		if(flag.equals("B")){//报废则+
			ret_wo.setScrp_prd_qty(ret_wo.getScrp_prd_qty() + scrap_qty);
		}else if(flag.equals("C")){//取消则-
			ret_wo.setScrp_prd_qty(ret_wo.getScrp_prd_qty() - scrap_qty);
		}
		ret_wo.setEvt_usr(evt_user);
		ret_wo.setEvt_timestamp(evt_timestamp);
		retWoDao.update(ret_wo);
		// update so scrap count
//		Ret_wo ret_so = retWoDao.get(so_id);
//		if (null == ret_so) {
//			rtnBeen.setMsgBuf(new NotFoundRtnBeen(so_id, Ret_wo.class).toString());
//			rtnBeen.setlRc(E_RET_WO + E_READ_NOT_FOUND);
//			return rtnBeen;
//		}
//		//ret_so.setScrp_prd_qty(ret_so.getScrp_prd_qty() + 1);
//		if(flag.equals("B")) {
//			ret_so.setScrp_prd_qty(ret_so.getScrp_prd_qty() + scrap_qty);
//		}else if (flag.equals("C")){
//			ret_so.setScrp_prd_qty(ret_so.getScrp_prd_qty() - scrap_qty);
//		}
//		ret_so.setEvt_usr(evt_user);
//		ret_so.setEvt_timestamp(evt_timestamp);
//		retWoDao.update(ret_so);
		return new SimpleRtnBeen(_NORMAL);
	}

	private void setPrdScrapInfo(String evt_seq_id, FspprdscrapI inTrx, Ret_prd_info ret_prd_info, Timestamp evt_timestamp) {
		String evtUsr = inTrx.getEvt_usr();

		String rsnCate = inTrx.getRsn_cate();
		String rsnCode = inTrx.getRsn_code();
		String rsnDept = inTrx.getRsn_dept();
		String shtOpeMsg = inTrx.getSht_ope_msg();
		/*
		 * 向表ret_prd_abn插入数据
		 */
		Ret_prd_abnId id = new Ret_prd_abnId();
		id.setEvt_seq_id_fk(evt_seq_id);
		id.setPrd_seq_id_fk(ret_prd_info.getPrd_seq_id());

		Ret_prd_abn ret_prd_abn = retPrdAbnDao.get(id);
		if(ret_prd_abn == null){
			Ret_prd_abn newScrp = new Ret_prd_abn();
			newScrp.setId(id);
			if(StringUtils.isNotEmpty( inTrx.getBox_id())){
				newScrp.setBox_id_fk(inTrx.getBox_id());
			}

			newScrp.setPrd_stat(PROC_SCRP);
			newScrp.setPv_prd_stat(ret_prd_info.getPrd_stat());
			newScrp.setWo_id_fk(ret_prd_info.getWo_id_fk());

			newScrp.setCr_ope_id_fk(ret_prd_info.getCr_ope_id_fk());
			newScrp.setCr_ope_no_fk(ret_prd_info.getCr_ope_no_fk());
			newScrp.setCr_ope_ver_fk(ret_prd_info.getCr_ope_ver_fk());
			newScrp.setCr_path_id_fk(ret_prd_info.getCr_path_id_fk());
			newScrp.setCr_path_ver_fk(ret_prd_info.getCr_path_ver_fk());
			newScrp.setCr_tool_id_fk(ret_prd_info.getCr_tool_id_fk());
			newScrp.setCr_toolg_id_fk(ret_prd_info.getCr_toolg_id_fk());
			newScrp.setCr_proc_id_fk(ret_prd_info.getCr_proc_id_fk());
			newScrp.setCr_tool_run_mode(ret_prd_info.getCr_tool_run_mode());

			newScrp.setNx_ope_id_fk(ret_prd_info.getNx_ope_id_fk());
			newScrp.setNx_ope_no_fk(ret_prd_info.getNx_ope_no_fk());
			newScrp.setNx_ope_ver_fk(ret_prd_info.getNx_ope_ver_fk());
			newScrp.setNx_path_id_fk(ret_prd_info.getNx_path_id_fk());
			newScrp.setNx_path_ver_fk(ret_prd_info.getNx_path_ver_fk());
			newScrp.setNx_toolg_id_fk(ret_prd_info.getNx_toolg_id_fk());
			newScrp.setNx_proc_id_fk(ret_prd_info.getNx_proc_id_fk());
			newScrp.setNx_tool_run_mode(ret_prd_info.getNx_tool_run_mode());
			newScrp.setAct_code(ABN_ACTION_SCRAP);
			newScrp.setRsn_cate(rsnCate);
			newScrp.setRsn_code(rsnCode);
			newScrp.setRsn_dept(rsnDept);
			newScrp.setEvt_cate(EVT_PRD_SCRP);
			newScrp.setEvt_dept(ret_prd_info.getEvt_dept());
			newScrp.setEvt_usr(evtUsr);
			newScrp.setEvt_timestamp(evt_timestamp);
			retPrdAbnDao.save(newScrp);
		}

		/**
		 * 报废玻璃  set cr && nx -->space
		 */
		if (StringUtils.isNotBlank(ret_prd_info.getBox_id_fk())){
			ret_prd_info.setPv_box_id_fk(ret_prd_info.getBox_id_fk());
		}
		ret_prd_info.setBox_id_fk(_SPACE);
		ret_prd_info.setSlot_no(_SPACE);
		ret_prd_info.setPv_prd_stat_fk(ret_prd_info.getPrd_stat());
		ret_prd_info.setPrd_stat(PROC_SCRP);

		ret_prd_info.setCr_ope_id_fk(_SPACE);
		ret_prd_info.setCr_ope_no_fk(_SPACE);
		ret_prd_info.setCr_ope_ver_fk(_SPACE);
		ret_prd_info.setCr_path_id_fk(_SPACE);
		ret_prd_info.setCr_path_ver_fk(_SPACE);
		ret_prd_info.setCr_proc_id_fk(_SPACE);
		ret_prd_info.setCr_tool_run_mode(_SPACE);
		ret_prd_info.setCr_toolg_id_fk(_SPACE);
		ret_prd_info.setCr_tool_id_fk(_SPACE);
		ret_prd_info.setTool_id_fk(_SPACE);

		ret_prd_info.setNx_ope_id_fk(_SPACE);
		ret_prd_info.setNx_ope_no_fk(_SPACE);
		ret_prd_info.setNx_ope_ver_fk(_SPACE);
		ret_prd_info.setNx_path_id_fk(_SPACE);
		ret_prd_info.setNx_path_ver_fk(_SPACE);
		ret_prd_info.setNx_proc_id_fk(_SPACE);
		ret_prd_info.setNx_tool_run_mode(_SPACE);
		ret_prd_info.setNx_toolg_id_fk(_SPACE);
		ret_prd_info.setNx_tool_id_fk(_SPACE);

		ret_prd_info.setRsn_cate(rsnCate);
		ret_prd_info.setRsn_code(rsnCode);
		ret_prd_info.setRsn_dept(rsnDept);
		ret_prd_info.setEvt_cate(EVT_PRD_SCRP);
		ret_prd_info.setEvt_usr(evtUsr);
		ret_prd_info.setEvt_timestamp(evt_timestamp);
		ret_prd_info.setSht_ope_msg(shtOpeMsg);
		retPrdInfoDao.update(ret_prd_info);
	}
	/**
	 * Cancel Scrap
	 * @param inTrx
	 * @param outTrx
	 */
	public void cancelFnc(FspprdscrapI inTrx,FspprdscrapO outTrx){
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen();
		rtnBeen.setlRc(_NORMAL);
		List<FspprdscrapIA> iaryList = inTrx.getIary();
		String boxIdI = inTrx.getBox_id_cancel_in();
		Integer chip_qty =0;
		Ret_box ret_box = new Ret_box();
		List<Ret_prd_info> ret_prd_infos = new ArrayList<Ret_prd_info>();
		List<Ret_prd_abn> ret_prd_abn_arr = new ArrayList<Ret_prd_abn>();
		Timestamp evt_timestamp = Util.getCurrentTimestamp();

		for(int i =0;i<iaryList.size(); i++){
			String prd_seq_id = iaryList.get(i).getPrd_seq_id();
			if(StringUtils.isEmpty(prd_seq_id)){
				outTrx.setRtn_code(E_FSPPRDSCRP_OTHER_ERR + "");
				outTrx.setRtn_mesg("玻璃/面板["+prd_seq_id+"]为空，请确认!");
				return;
			}

			Ret_prd_info ret_prd_info = retPrdInfoDao.get(prd_seq_id);
			if(ret_prd_info == null){
				outTrx.setRtn_code(E_RET_PRD_INFO + E_READ_NOT_FOUND + "");
				outTrx.setRtn_mesg("玻璃/面板["+prd_seq_id+"]不存在，请确认!");
				return;
			}

			if(!PROC_SCRP.equals(ret_prd_info.getPrd_stat())){
				outTrx.setRtn_code(E_FSPPRDSCRP_OTHER_ERR + "");
				outTrx.setRtn_mesg("玻璃[" + prd_seq_id +"],状态为"+ret_prd_info.getPrd_stat()+",不可进行取消报废操作，请重新选择玻璃!");
				return;
			}

//			if( PROC_WAIT.equals(ret_prd_info.getPv_prd_stat_fk()) ){
//				if(StringUtils.isEmpty(boxIdI)){
//					outTrx.setRtn_code(E_FSPPRDSCRP_OTHER_ERR + "");
//					outTrx.setRtn_mesg("玻璃[" + prd_seq_id +"],上次状态为"+ret_prd_info.getPv_prd_stat_fk()+",必须输入转入卡匣代码！");
//					return;
//				}
//			}


			String hql = "FROM Ret_prd_abn where prd_seq_id_fk='"+prd_seq_id + "' and act_code='" + ABN_ACTION_SCRAP +"'";
			List<Ret_prd_abn> ret_prd_abns = retPrdAbnDao.find(hql);

			if(ret_prd_abns == null || ret_prd_abns.size() == 0 ){
				outTrx.setRtn_code(E_FSPPRDSCRP_OTHER_ERR + "");
				outTrx.setRtn_mesg("报废表中没有找到对应["+ret_prd_info.getPrd_seq_id()+"]玻璃的数据！");
				return;
			}
			if(ret_prd_info.getPv_prd_stat_fk().equals(PROC_WAIT)){
				ret_prd_infos.add(ret_prd_info);
				ret_prd_abn_arr.add(ret_prd_abns.get(0));
				chip_qty += ret_prd_info.getPrd_qty();
			}

			/**
			 * set ret_prd_info data
			 * delete ret_prd_abn data
			 */
			setPrdCancelScrapInfo(inTrx, ret_prd_info, ret_prd_abns.get(0), evt_timestamp);
		}
		/**
		 * Check prd info is same or not
		 */
		if(ret_prd_infos.size()>0){
/*			rtnBeen = checkBoxInfo(boxIdI);
			if (rtnBeen.getlRc() != _NORMAL) {
				outTrx.setRtn_code(rtnBeen.getlRc() + "");
				outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
				return;
			}else{
				ret_box = (Ret_box) rtnBeen.getObj();
			}*/
			/**
			 * check SCRP-->WAIT PRD INFO
			 */
//			rtnBeen = checkPrdInfo(ret_prd_infos, ret_prd_abn_arr);
//			if (rtnBeen.getlRc() != _NORMAL) {
//				outTrx.setRtn_code(rtnBeen.getlRc() + "");
//				outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
//				return;
//			}

			/**
			 * set ret_box data
			 */
/*			rtnBeen = setRetBoxInfo(ret_box, chip_qty, inTrx, evt_timestamp);
			if (rtnBeen.getlRc() != _NORMAL) {
				outTrx.setRtn_code(rtnBeen.getlRc() + "");
				outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
				return;
			}*/
			//update wo && so scrap qty
			rtnBeen = updateWoSoScrapQty("C",ret_prd_infos.get(0).getWo_id_fk(), ret_prd_infos.get(0).getSo_id_fk(),
					ret_prd_infos.size(), evt_timestamp, inTrx.getEvt_usr());
			if (rtnBeen.getlRc() != _NORMAL) {
				outTrx.setRtn_code(rtnBeen.getlRc()+"");
				outTrx.setRtn_mesg(rtnBeen.getMsgBuf());
				return;
			}
		}

		outTrx.setRtn_code(RETURN_CODE_OK);
		outTrx.setRtn_mesg(RETURN_CODE_OK);
		return;
	}


	private SimpleRtnBeen checkBoxInfo(String boxIdI) {
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen();
		rtnBeen.setlRc(_NORMAL);
		String gszMsgBuf = "";
		if(StringUtils.isEmpty(boxIdI)){
			gszMsgBuf = "卡匣代码["+boxIdI+"]为空，请确认!";
			return new SimpleRtnBeen(E_FSPPRDSCRP_OTHER_ERR, gszMsgBuf);
		}
		Ret_box ret_box = retBoxDao.get(boxIdI);
		if(ret_box == null){
			gszMsgBuf = "卡匣["+boxIdI+"]不存在，请确认!";
			return new SimpleRtnBeen(E_RET_BOX + E_READ_NOT_FOUND, gszMsgBuf);
		}
		if(!PROC_EMPTY.equals(ret_box.getBox_stat().trim())){
			gszMsgBuf = "转入卡匣["+boxIdI+"]状态为["+ret_box.getBox_stat().trim()+"],必须是空卡匣!";
			return new SimpleRtnBeen(E_FSPPRDSCRP_OTHER_ERR, gszMsgBuf);
		}
		if(!ret_box.getBox_cate().trim().equals(BOX_CATE_WCST)){

			gszMsgBuf = "卡匣类型[" + ret_box.getBox_cate() + "]不正确，必须为["+BOX_CATE_WCST+"!";
			return new SimpleRtnBeen(E_FSPPRDSCRP_OTHER_ERR, gszMsgBuf);
		}
		rtnBeen.setObj(ret_box);
		return rtnBeen;
	}

	private SimpleRtnBeen checkPrdInfo(List<Ret_prd_info> ret_prd_infos, List<Ret_prd_abn> ret_prd_abn_arr) {
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen();
		rtnBeen.setlRc(_NORMAL);
		String gszMsgBuf = "";
		for(int i=0;i<ret_prd_infos.size();i++){
			Ret_prd_info ret_prd_info = ret_prd_infos.get(i);
			Ret_prd_abn ret_prd_abn = ret_prd_abn_arr.get(i);


			if (!ret_prd_info.getMdl_id_fk().equals(
					ret_prd_infos.get(i).getMdl_id_fk())) {
				gszMsgBuf = "玻璃的产品ID不能混！[" + ret_prd_info.getPrd_seq_id() + "]["
						+ ret_prd_info.getMdl_id_fk() + "] vs ["
						+ ret_prd_infos.get(i).getPrd_seq_id() + "]["
						+ ret_prd_infos.get(i).getMdl_id_fk() + "] ";
				return new SimpleRtnBeen(SHT_MDL_ID_MIX, gszMsgBuf);
			}

			// Path in
			if (!ret_prd_abn.getNx_path_id_fk().equals(
					ret_prd_abn_arr.get(i).getNx_path_id_fk())) {
				gszMsgBuf = "玻璃的下一工艺路线不能混！[" + ret_prd_info.getPrd_seq_id()
						+ "][" + ret_prd_abn.getNx_path_id_fk() + "] vs ["
						+ ret_prd_abn_arr.get(i).getId().getPrd_seq_id_fk() + "]["
						+ ret_prd_abn_arr.get(i).getNx_path_id_fk() + "] ";
				return new SimpleRtnBeen(SHT_PATH_ID_MIX, gszMsgBuf);
			}
			if (!ret_prd_abn.getNx_path_ver_fk().equals(
					ret_prd_abn_arr.get(i).getNx_path_ver_fk())) {
				gszMsgBuf = "玻璃的下一工艺路线版本不能混！ [" + ret_prd_info.getPrd_seq_id()
						+ "][" + ret_prd_abn.getNx_path_ver_fk() + "] vs ["
						+ ret_prd_abn_arr.get(i).getId().getPrd_seq_id_fk() + "]["
						+ ret_prd_abn_arr.get(i).getNx_path_ver_fk() + "] ";
				return new SimpleRtnBeen(SHT_PATH_VER_MIX, gszMsgBuf);
			}

			if (!ret_prd_abn.getNx_ope_id_fk().equals(
					ret_prd_abn_arr.get(i).getNx_ope_id_fk())) {
				gszMsgBuf = "玻璃的下一站点ID不能混！ [" + ret_prd_info.getPrd_seq_id()
						+ "][" + ret_prd_abn.getNx_ope_id_fk() + "] vs ["
						+ ret_prd_abn_arr.get(i).getId().getPrd_seq_id_fk() + "]["
						+ ret_prd_abn_arr.get(i).getNx_ope_id_fk() + "] ";
				return new SimpleRtnBeen(SHT_OPE_ID_MIX, gszMsgBuf);
			}
			if (!ret_prd_abn.getNx_ope_no_fk().equals(
					ret_prd_abn_arr.get(i).getNx_ope_no_fk())) {
				gszMsgBuf = "玻璃的下一站点编号不能混！ [" + ret_prd_abn.getId().getPrd_seq_id_fk()
						+ "][" + ret_prd_abn.getNx_ope_no_fk() + "] vs ["
						+ ret_prd_abn_arr.get(i).getId().getPrd_seq_id_fk() + "]["
						+ ret_prd_abn_arr.get(i).getNx_ope_no_fk() + "] ";
				return new SimpleRtnBeen(SHT_OPE_NO_MIX, gszMsgBuf);
			}
			if (!ret_prd_abn.getNx_ope_ver_fk().equals(
					ret_prd_abn_arr.get(i).getNx_ope_ver_fk())) {
				gszMsgBuf = "玻璃的下一站点版本不能混！[" + ret_prd_info.getPrd_seq_id()
						+ "][" + ret_prd_abn.getNx_ope_ver_fk() + "] vs ["
						+ ret_prd_abn_arr.get(i).getId().getPrd_seq_id_fk() + "]["
						+ ret_prd_abn_arr.get(i).getNx_ope_ver_fk() + "] ";
				return new SimpleRtnBeen(SHT_OPE_VER_MIX, gszMsgBuf);
			}
			//check pf count
			if(Integer.valueOf(ret_prd_info.getPrd_pf_cnt()) != Integer.valueOf(ret_prd_infos.get(i).getPrd_pf_cnt()) ){
				gszMsgBuf = "玻璃的拔膜次数不能混！ [" + ret_prd_info.getPrd_seq_id() + "]["
						+ ret_prd_info.getPrd_pf_cnt() + "] vs ["
						+ ret_prd_infos.get(i).getPrd_seq_id() + "]["
						+ ret_prd_infos.get(i).getPrd_pf_cnt() + "] ";
				return new SimpleRtnBeen(SHT_PF_COUNT_MIX, gszMsgBuf);
			}

			// WO info
			if (!ret_prd_info.getWo_id_fk().equals(
					ret_prd_infos.get(i).getWo_id_fk())) {
				gszMsgBuf = "玻璃的工单号不能混！ [" + ret_prd_info.getPrd_seq_id() + "]["
						+ ret_prd_info.getWo_id_fk() + "] vs ["
						+ ret_prd_infos.get(i).getPrd_seq_id() + "]["
						+ ret_prd_infos.get(i).getWo_id_fk() + "] ";
				return new SimpleRtnBeen(SHT_WO_ID_MIX, gszMsgBuf);
			}
		}

		return rtnBeen;
	}


	private SimpleRtnBeen setRetBoxInfo(Ret_box ret_box, Integer chip_qty, FspprdscrapI inTrx, Timestamp evt_timestamp) {
		SimpleRtnBeen rtnBeen = new SimpleRtnBeen();
		rtnBeen.setlRc(_NORMAL);
		String gszMsgBuf = "";
		/**
		 * Check Box Information
		 */
		//check box max count
		String hql = "From Bis_data where data_cate='BXSC' AND data_ext='"+ret_box.getBox_set_code()+"'";
		List<Bis_data> bisDataList = bisDataDao.find(hql);
		int maxCnt = Integer.valueOf( bisDataList.get(0).getExt_1() );
		List<Ret_prd_info> ret_prd_infos = retPrdInfoDao.getShtListInfo(ret_box.getBox_id());
		if(ret_prd_infos.size()>maxCnt){
			gszMsgBuf = "取消报废的玻璃片数为["+ret_prd_infos.size()+"],但卡匣["+ret_box.getBox_id()
					+"]最大可以放入玻璃片数为["+maxCnt+"],请确认！";
			return new SimpleRtnBeen(E_FSPPRDSCRP_OTHER_ERR, gszMsgBuf);
		}
		/**
		 * set box information
		 */
		Ret_prd_info prd = ret_prd_infos.get(0);

		ret_box.setPrd_qty(ret_prd_infos.size());
		ret_box.setChip_qty(ret_box.getChip_qty() + chip_qty);
		ret_box.setBox_stat(PROC_WAIT);
		ret_box.setNx_ope_id_fk(prd.getNx_ope_id_fk());
		ret_box.setNx_ope_no_fk(prd.getNx_ope_no_fk());
		ret_box.setNx_ope_ver_fk(prd.getNx_ope_ver_fk());
		ret_box.setNx_path_id_fk(prd.getNx_path_id_fk());
		ret_box.setNx_path_ver_fk(prd.getNx_path_ver_fk());
		ret_box.setNx_proc_id_fk(prd.getNx_proc_id_fk());
		ret_box.setNx_tool_run_mode(prd.getNx_tool_run_mode());
		ret_box.setNx_toolg_id_fk(prd.getNx_toolg_id_fk());

		ret_box.setMdl_id_fk(prd.getMdl_id_fk());
		ret_box.setMtrl_prod_id_fk(prd.getMtrl_prod_id_fk());
		ret_box.setWo_id_fk(prd.getWo_id_fk());
		ret_box.setSo_id_fk(prd.getSo_id_fk());
		ret_box.setEvt_cate(EVT_PRD_SCRAP_CANCEL);
		ret_box.setEvt_usr(inTrx.getEvt_usr());
		ret_box.setEvt_timestamp(evt_timestamp);
		retBoxDao.update(ret_box);
		return rtnBeen;
	}

	private void setPrdCancelScrapInfo(FspprdscrapI inTrx, Ret_prd_info ret_prd_info,
									   Ret_prd_abn ret_prd_abn, Timestamp evt_timestamp) {
		//unload端玻璃没有卡匣信息
		if(ret_prd_info.getPv_prd_stat_fk().equals(PROC_WAIT)){
			ret_prd_info.setBox_id_fk(inTrx.getBox_id_cancel_in());
		}
		ret_prd_info.setPv_prd_stat_fk(ret_prd_info.getPrd_stat());
		ret_prd_info.setPrd_stat(ret_prd_abn.getPv_prd_stat());
		ret_prd_info.setCr_ope_id_fk(ret_prd_abn.getCr_ope_id_fk());
		ret_prd_info.setCr_ope_no_fk(ret_prd_abn.getCr_ope_no_fk());
		ret_prd_info.setCr_ope_ver_fk(ret_prd_abn.getCr_ope_ver_fk());
		ret_prd_info.setCr_path_id_fk(ret_prd_abn.getCr_path_id_fk());
		ret_prd_info.setCr_path_ver_fk(ret_prd_abn.getCr_path_ver_fk());
		ret_prd_info.setCr_toolg_id_fk(ret_prd_abn.getCr_toolg_id_fk());
		ret_prd_info.setCr_tool_id_fk(ret_prd_abn.getCr_tool_id_fk());
		ret_prd_info.setCr_proc_id_fk(ret_prd_abn.getCr_proc_id_fk());
		ret_prd_info.setCr_tool_run_mode(ret_prd_abn.getCr_tool_run_mode());


		ret_prd_info.setNx_ope_id_fk(ret_prd_abn.getNx_ope_id_fk());
		ret_prd_info.setNx_ope_no_fk(ret_prd_abn.getNx_ope_no_fk());
		ret_prd_info.setNx_ope_ver_fk(ret_prd_abn.getNx_ope_ver_fk());
		ret_prd_info.setNx_path_id_fk(ret_prd_abn.getNx_path_id_fk());
		ret_prd_info.setNx_path_ver_fk(ret_prd_abn.getNx_path_id_fk());
		ret_prd_info.setNx_path_ver_fk(ret_prd_abn.getNx_path_ver_fk());
		ret_prd_info.setNx_toolg_id_fk(ret_prd_abn.getNx_toolg_id_fk());
		ret_prd_info.setNx_proc_id_fk(ret_prd_abn.getNx_proc_id_fk());
		ret_prd_info.setNx_tool_run_mode(ret_prd_abn.getNx_tool_run_mode());

		String slot_no = creatNewSlotNo(inTrx.getBox_id_cancel_in());
		ret_prd_info.setSlot_no(slot_no);
		ret_prd_info.setEvt_cate(EVT_PRD_SCRAP_CANCEL);
		ret_prd_info.setEvt_timestamp(evt_timestamp);
		ret_prd_info.setEvt_usr(inTrx.getEvt_usr());
		ret_prd_info.setSht_ope_msg(inTrx.getSht_ope_msg());
		ret_prd_info.setRsn_cate(_SPACE);
		ret_prd_info.setRsn_code(_SPACE);
		ret_prd_info.setRsn_dept(_SPACE);
		retPrdInfoDao.update(ret_prd_info);

		/**
		 * delete ret_prd_abn data
		 */
		retPrdAbnDao.delete(ret_prd_abn);
	}

	/**
	 * Create New Slot No
	 * @param //slot_no_max
	 * @return
	 */
	private String creatNewSlotNo(String box_id){
		String slot_no_max = "", slot_no = "";
		int slot_no_max_int = 0, slot_no_new_int = 0;
		String sql = "SELECT MAX(SLOT_NO) From Ret_prd_info WHERE BOX_ID_FK='"+box_id+"'";
		List<String> objlist = retPrdInfoDao.findBySQL(sql);
		if(objlist.get(0) == null || objlist.size()==0){
			slot_no = "01";
		}else{
			slot_no_max = objlist.get(0);
			slot_no_max_int = Integer.valueOf(slot_no_max);
			slot_no_new_int = slot_no_max_int + 1;
			slot_no = String.format("%02d", slot_no_new_int);
		}

		return slot_no;
	}

	//错误信息
	public void errActionFnc(FspprdscrapO outTrx) {
		outTrx.setRtn_code("0000009");
		outTrx.setRtn_mesg("Invalid Action_flg");
	}
}

